import Reaction from './reaction';
function deepProxy(val,handler){
 if(typeof val !== 'object') return val;
 for(let key in val){
   val[key] = deepProxy(val[key],handler);
 }
 return new Proxy(val,handler())
}

function createObservable(val){
  //声明一个专门用来代理的对象
  let handler = ()=>{
    let reaction = new Reaction();
    return {
      set(target,key,value){
        if(key === 'length') return true;
        let r = Reflect.set(target,key,value);
        reaction.run();
        return r;
      },
      get(target,key){
         reaction.collect();
         return Reflect.get(target,key);
      }
    }
  }
  return deepProxy(val,handler);
}


export default function observable(target){
// 需要将此对象代理 创建成可观察对象
  return createObservable(target);
}